package handler

import (
	"fitness/go-admin/internal/model"
	"fitness/go-admin/internal/service"
	"fitness/go-admin/pkg/response"
	"strconv"

	"github.com/gin-gonic/gin"
)

// SensitiveWordHandler 敏感词处理器
type SensitiveWordHandler struct {
	service service.SensitiveWordService
}

// NewSensitiveWordHandler 创建敏感词处理器
func NewSensitiveWordHandler(service service.SensitiveWordService) *SensitiveWordHandler {
	return &SensitiveWordHandler{
		service: service,
	}
}

// CreateSensitiveWord 创建敏感词
// @Summary 创建敏感词
// @Description 添加敏感词到系统（仅管理员）
// @Tags 敏感词管理
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body model.CreateSensitiveWordReq true "创建请求"
// @Success 200 {object} response.Response{data=model.SensitiveWord}
// @Router /api/v1/sensitive-words [post]
func (h *SensitiveWordHandler) CreateSensitiveWord(c *gin.Context) {
	userID := c.GetUint("userID")

	var req model.CreateSensitiveWordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, 400, "参数错误")
		return
	}

	word, err := h.service.CreateSensitiveWord(userID, &req)
	if err != nil {
		response.Error(c, 400, err.Error())
		return
	}

	response.Success(c, word, "创建成功")
}

// GetSensitiveWord 获取敏感词详情
// @Summary 获取敏感词详情
// @Description 获取单个敏感词的详细信息
// @Tags 敏感词管理
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "敏感词ID"
// @Success 200 {object} response.Response{data=model.SensitiveWord}
// @Router /api/v1/sensitive-words/{id} [get]
func (h *SensitiveWordHandler) GetSensitiveWord(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.Error(c, 400, "ID格式错误")
		return
	}

	word, err := h.service.GetSensitiveWord(uint(id))
	if err != nil {
		response.Error(c, 404, err.Error())
		return
	}

	response.Success(c, word, "")
}

// GetSensitiveWordList 获取敏感词列表
// @Summary 获取敏感词列表
// @Description 获取敏感词列表，支持筛选和搜索
// @Tags 敏感词管理
// @Accept json
// @Produce json
// @Security Bearer
// @Param category query string false "分类筛选"
// @Param level query string false "级别筛选"
// @Param isEnabled query bool false "是否启用"
// @Param keyword query string false "关键词搜索"
// @Param page query int false "页码"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} response.Response{data=model.SensitiveWordListResp}
// @Router /api/v1/sensitive-words [get]
func (h *SensitiveWordHandler) GetSensitiveWordList(c *gin.Context) {
	var req model.SensitiveWordListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, 400, "参数错误")
		return
	}

	result, err := h.service.GetSensitiveWordList(&req)
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, result, "")
}

// UpdateSensitiveWord 更新敏感词
// @Summary 更新敏感词
// @Description 更新敏感词信息（仅管理员）
// @Tags 敏感词管理
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "敏感词ID"
// @Param request body model.UpdateSensitiveWordReq true "更新请求"
// @Success 200 {object} response.Response
// @Router /api/v1/sensitive-words/{id} [put]
func (h *SensitiveWordHandler) UpdateSensitiveWord(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.Error(c, 400, "ID格式错误")
		return
	}

	var req model.UpdateSensitiveWordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, 400, "参数错误")
		return
	}

	if err := h.service.UpdateSensitiveWord(uint(id), &req); err != nil {
		response.Error(c, 400, err.Error())
		return
	}

	response.Success(c, nil, "更新成功")
}

// DeleteSensitiveWord 删除敏感词
// @Summary 删除敏感词
// @Description 删除敏感词（仅管理员）
// @Tags 敏感词管理
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "敏感词ID"
// @Success 200 {object} response.Response
// @Router /api/v1/sensitive-words/{id} [delete]
func (h *SensitiveWordHandler) DeleteSensitiveWord(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.Error(c, 400, "ID格式错误")
		return
	}

	if err := h.service.DeleteSensitiveWord(uint(id)); err != nil {
		response.Error(c, 400, err.Error())
		return
	}

	response.Success(c, nil, "删除成功")
}

// ReloadSensitiveWords 重新加载敏感词缓存
// @Summary 重新加载敏感词
// @Description 从数据库重新加载敏感词缓存（仅管理员）
// @Tags 敏感词管理
// @Accept json
// @Produce json
// @Security Bearer
// @Success 200 {object} response.Response
// @Router /api/v1/sensitive-words/reload [post]
func (h *SensitiveWordHandler) ReloadSensitiveWords(c *gin.Context) {
	if err := h.service.ReloadSensitiveWords(); err != nil {
		response.Error(c, 500, "重新加载失败: "+err.Error())
		return
	}

	response.Success(c, nil, "重新加载成功")
}

